﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xClient.Framework.StaticResource;

namespace cMES1006
{
    public partial class frmLineBaseEdit : Form
    {
        private DataGridViewRow _dgvRow = null;
        private string _strChangeMode = "ADD";
        public frmLineBaseEdit(string command,  object model =null)
        {
            InitializeComponent();
            this._strChangeMode = command;
            if (_strChangeMode.Equals("ADD"))
            {
                this.Text = "新增线体基础信息";
                //清除当前页面数据
                this.ClearFormInputData();
            }
            else if (_strChangeMode.Equals("EDIT"))
            {
                this.Text = "修改线体基础信息";
                //if (_strChangeMode.Equals("VIEW"))
                //{
                //    this.Text = "查看线体基础信息";
                //    this.DisableInputForDataView();
                //}
                this.lblId.Visible = true;
                this.txtId.Visible = true;
                this.txtId.Enabled = false;
                //绑定页面数据
                if (model != null)
                {
                    _dgvRow = (DataGridViewRow)model;
                    this.txtId.Text = _dgvRow.Cells["line_id"].Value.ToString();
                    this.txtLineName.Text = _dgvRow.Cells["line_name"].Value.ToString();
                    this.combShift.Text = _dgvRow.Cells["shift"].Value.ToString();
                    this.txtLineSpeed.Text = _dgvRow.Cells["line_speed"].Value.ToString();
                    this.txtAnnouncement.Text = _dgvRow.Cells["announcement"].Value.ToString();
                    this.txtHCBudget.Text = _dgvRow.Cells["hc_budget"].Value.ToString();
                    this.txtHCActual.Text = _dgvRow.Cells["hc_actual"].Value.ToString();
                    this.txtAbsent.Text = _dgvRow.Cells["absent"].Value.ToString();
                    this.txtLineDescription.Text = _dgvRow.Cells["line_description"].Value.ToString();
                    this.txtRemarks.Text = _dgvRow.Cells["remarks"].Value.ToString();
                    this.txtCreateUser.Text = _dgvRow.Cells["create_user"].Value.ToString();
                    this.dtpCreateTime.text = _dgvRow.Cells["create_time"].Value.ToString();
                    this.txtUpdateUser.Text = _dgvRow.Cells["update_user"].Value.ToString(); 
                    this.dtpUpdateTime.text  = _dgvRow.Cells["update_time"].Value.ToString();
                    var lineManager = (dynamic)_dgvRow.Cells["linemanager"].Value;
                    if (lineManager != null)
                    {
                        this.txtLineManager.Text = lineManager.line_manager;
                        this.txtManagerTelNo.Text = lineManager.manager_tel_no;
                        this.txtManagerEmail.Text = lineManager.manager_email;
                        if(lineManager.manager_picture != null)
                        {
                            this.picbManager.Image = null;
                            this.picbManager.WaitOnLoad = false;
                            this.picbManager.SizeMode = PictureBoxSizeMode.Zoom;
                            this.picbManager.LoadAsync(Convert.ToString(lineManager.manager_picture));                            
                        }
                       
                        this.txtLineLeader.Text = lineManager.line_leader;
                        this.txtLeaderTelNo.Text = lineManager.leader_tel_no;
                        this.txtLeaderEmail.Text = lineManager.leader_email;
                        if (lineManager.leader_picture != null)
                        {
                            this.picbLeader.Image = null;
                            this.picbLeader.WaitOnLoad = false;
                            this.picbLeader.SizeMode = PictureBoxSizeMode.Zoom;
                            this.picbLeader.LoadAsync(Convert.ToString(lineManager.leader_picture));                            
                        }
                        
                        this.txtLineMngrRemarks.Text = lineManager.remarks;
                    }
                }
            }
        }
        private void ClearFormInputData()
        {
            this.lblId.Visible = false;
            this.txtId.Visible = false;
            this.txtId.Text = "";
            this.txtLineName.Text = "";
            this.combShift.Text = "";
            this.txtLineSpeed.Text = "";
            this.txtHCBudget.Text = "";
            this.txtHCActual.Text = "";
            this.txtAbsent.Text = "";
            this.txtLineDescription.Text = "";
            this.txtRemarks.Text = "";
            this.txtCreateUser.Text = StaticDictionary.Dictionary.UserID;
            this.dtpCreateTime.Text = DateTime.Now.ToLongTimeString();
            this.txtUpdateUser.Text = StaticDictionary.Dictionary.UserID;
            this.dtpUpdateTime.text = DateTime.Now.ToLongTimeString();
            this.txtLineManager.Text = "";
            this.txtManagerTelNo.Text = "";
            this.txtManagerEmail.Text = "";
            this.txtLineLeader.Text = "";
            this.txtLeaderTelNo.Text = "";
            this.txtLeaderEmail.Text = "";
            this.txtLineDescription.Text = "";
        }
        private bool CheckInputDataValidate()
        {
            bool result = true;
            if (string.IsNullOrEmpty(this.txtLineName.Text.ToUpper().Trim()))
            {                
                MessageBox.Show("线体名称必须输入");
                return result = false;
            }
            if(string.IsNullOrEmpty(this.combShift.Text.ToUpper().Trim()))
            {                
                MessageBox.Show("班别必须选择");
                return result = false;
            }
            try
            {
                int _iHCBgt = Convert.ToInt32(this.txtHCBudget.Text.Trim());
                int _iHCAct = Convert.ToInt32(this.txtHCActual.Text.Trim());
                int _iHCAbs = Convert.ToInt32(this.txtAbsent.Text.Trim());

            }
            catch (Exception)
            {
                result = false;
                MessageBox.Show("输入错误，人力编制、实际人力、缺勤 必须为数字。");
                return result = false;
            }

            return result;
        }

        private void DisableInputForDataView()
        {
            this.txtId.Enabled = false;
            this.txtId.Visible = true;
            this.txtLineName.Enabled = false;
            this.combShift.Enabled = false;
            this.txtLineSpeed.Enabled = false;
            this.txtAnnouncement.Enabled = false;
            this.txtHCBudget.Enabled = false;
            this.txtHCActual.Enabled = false;
            this.txtAbsent.Enabled = false;
            this.txtLineDescription.Enabled = false;
            this.txtRemarks.Enabled = false;
            this.txtCreateUser.Enabled = false;
            this.dtpCreateTime.Enabled = false;
            this.txtUpdateUser.Enabled = false;
            this.dtpUpdateTime.Enabled = false;
            this.txtLineManager.Enabled = false;
            this.txtManagerTelNo.Enabled = false;
            this.txtManagerEmail.Enabled = false;
            this.picbManager.Enabled = false;
            this.txtLineLeader.Enabled = false;
            this.txtLeaderTelNo.Enabled = false;
            this.txtLeaderEmail.Enabled = false;
            this.picbLeader.Enabled = false;
            this.txtLineMngrRemarks.Enabled = false;
            this.btnAddLeaderPic.Visible = false;
            this.btnAddManagerPic.Visible = false;
            this.btnSave.Visible = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckInputDataValidate())
            {
                this.txtLineName.Focus();
                return;
            }
            #region line params
            var lineMngr = new Dictionary<string, object>();
            if (!String.IsNullOrEmpty(this.txtLineManager.Text.Trim()) &&
                !String.IsNullOrEmpty(this.txtLineLeader.Text.Trim()))
            {
                lineMngr.Add("line_manager", this.txtLineManager.Text.Trim());
                lineMngr.Add("manager_tel_no", this.txtManagerTelNo.Text.Trim());
                lineMngr.Add("manager_email", this.txtManagerEmail.Text);
                lineMngr.Add("manager_picture", this.picbManager.ImageLocation);
                lineMngr.Add("line_leader", this.txtLineLeader.Text.Trim());
                lineMngr.Add("leader_tel_no", this.txtLeaderTelNo.Text.Trim());
                lineMngr.Add("leader_email", this.txtLeaderEmail.Text.Trim());
                lineMngr.Add("leader_picture", this.picbLeader.ImageLocation);
                lineMngr.Add("remarks", this.txtLineMngrRemarks.Text.Trim());
            }
            
            var lineDatas = new Dictionary<string, object>();
            lineDatas.Add("line_name", this.txtLineName.Text.Trim().ToUpper());
            lineDatas.Add("shift", this.combShift.Text.Trim().ToUpper());
            lineDatas.Add("line_speed", this.txtLineSpeed.Text.Trim());
            lineDatas.Add("announcement", this.txtAnnouncement.Text.Trim());
            lineDatas.Add("hc_budget", this.txtHCBudget.Text.Trim());
            lineDatas.Add("hc_actual", this.txtHCActual.Text.Trim());
            lineDatas.Add("absent", this.txtAbsent.Text.Trim());
            lineDatas.Add("line_description", this.txtLineDescription.Text.Trim());
            lineDatas.Add("remarks", this.txtRemarks.Text.Trim());
            #endregion

            if (this._strChangeMode.Equals("ADD"))
            {
                //add new data
                lineMngr.Add("create_user", StaticDictionary.Dictionary.UserID);
                lineDatas.Add("create_user", StaticDictionary.Dictionary.UserID);
                lineDatas.Add("linemanager", lineMngr);

                //var dataModel = JsonConvert.SerializeObject(lineDatas);
                var result = Services.ServiceMethod.AddLineBase(lineDatas);
                if (result.Status)
                {
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    
                    MessageBox.Show(result.Message, "系统消息");
                    return;
                }
            }
            else
            {
                //modify data
                #region initial input data

                var lineMgr = (dynamic)_dgvRow.Cells["linemanager"].Value;
                if (lineMgr != null)
                {
                    lineMngr.Add("id", lineMgr.id);
                    lineMngr.Add("line_id", lineMgr.line_id);
                    lineMngr.Add("is_valid", lineMgr.is_valid);
                    lineMngr.Add("create_user", lineMgr.create_user);
                    lineMngr.Add("create_time", lineMgr.create_time);
                    lineMngr.Add("update_user", StaticDictionary.Dictionary.UserID);
                }
                else
                {
                    lineMngr.Add("line_id", this.txtId.Text);
                    lineMngr.Add("create_user", StaticDictionary.Dictionary.UserID);
                    lineMngr.Add("create_time", DateTime.Now);
                }

                lineDatas.Add("line_id", this.txtId.Text);
                lineDatas.Add("is_valid", _dgvRow.Cells["is_valid"].Value.ToString());
                lineDatas.Add("create_user", _dgvRow.Cells["create_user"].Value.ToString());
                lineDatas.Add("create_time", _dgvRow.Cells["create_time"].Value.ToString());
                lineDatas.Add("update_user", StaticDictionary.Dictionary.UserID);
                lineDatas.Add("linemanager", lineMngr);
                #endregion
                //var dataModel = JsonConvert.SerializeObject(lineDatas);

                var result = Services.ServiceMethod.UpdateLineBase(lineDatas);
                if (result.Status)
                {
                    this.DialogResult = DialogResult.OK;
                }
                else
                {                   
                    MessageBox.Show(result.Message, "系统消息");
                    return;
                }

            }
        }

        private void btnAddManagerPic_Click(object sender, EventArgs e)
        {
            OpenFileDialog _managerFBD = new OpenFileDialog();
            _managerFBD.Multiselect = false;
            _managerFBD.Filter = "图片文件(*.jpg,*.png,*.bmp)|*.jpg|*.png|*.bmp";
            DialogResult dialogResult = _managerFBD.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                string _filePath = _managerFBD.FileName;
                this.picbManager.Image = Image.FromFile(_filePath);
               // this.picbManager.ImageLocation = _filePath;
                var result = Services.ServiceMethod.UploadFileToServer(_filePath, this.txtLineName.Text.Trim());
                if (result.Status)
                {
                    _filePath = result.Data.ToString();
                    this.picbManager.LoadAsync(_filePath);
                    MessageBox.Show("上传文件成功。", "系统提示");
                }
            }
        }

        private void btnAddLeaderPic_Click(object sender, EventArgs e)
        {
            OpenFileDialog _leaderFBD = new OpenFileDialog();
            _leaderFBD.Multiselect = false;
            _leaderFBD.Filter = "图片文件(*.jpg,*.png,*.bmp)|*.jpg|*.png|*.bmp";
            DialogResult dialogResult = _leaderFBD.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                string _filePath = _leaderFBD.FileName;
                this.picbLeader.Image = Image.FromFile(_filePath);
                //this.picbLeader.ImageLocation = _filePath;
                var result = Services.ServiceMethod.UploadFileToServer(_filePath, this.txtLineName.Text.Trim());
                if (result.Status)
                {
                    _filePath = result.Data.ToString();
                    this.picbLeader.LoadAsync(_filePath);
                    MessageBox.Show("上传文件成功。", "系统提示");
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
